package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
/**
* 攻略明细服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repsitory;

    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        //根据主题id查询主题对象
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        //设置主题姓名保存
        strategy.setThemeName(strategyTheme.getName());
        repsitory.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repsitory.save(strategy);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,pageable, query,Strategy.class, qo);
    }

    @Override
    public List<Strategy> queryByCatalogId(String id) {
        return repsitory.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryViewNum(String destId) {

        Pageable pageable = PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");

        List<Strategy> strategies = repsitory.findByDestId(destId,pageable);


        return strategies;
    }


    @Override
    public void saveOrUpdate(Strategy strategy) {
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getId());
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        strategy.setCatalogName(catalog.getName());
        StrategyTheme theme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(theme.getName());
        List<Destination> toasts = destinationService.queryToasts(catalog.getDestId());
        if (toasts != null && toasts.size() > 0) {
            Destination destination = toasts.get(0);
            if ("中国".equals(destination.getName())) {
                strategy.setIsabroad(Strategy.ABROAD_NO);
            } else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        if(StringUtils.hasLength(strategy.getId())){
            //update
            Strategy temp = this.get(strategy.getId());
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setViewnum(temp.getViewnum());
            strategy.setReplynum(temp.getReplynum());
            strategy.setFavornum(temp.getFavornum());
            strategy.setSharenum(temp.getSharenum());
            strategy.setThumbsupnum(temp.getThumbsupnum());
            this.update(strategy);
        }else{
            //save
            //创建时间
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }
    }
}
